Lab 34: Copy Data to S3 Using Terraform
The Nautilus DevOps team is presently immersed in data migrations, transferring data from on-premise storage systems to AWS S3 buckets. They have recently received some data that they intend to copy to one of the S3 buckets.
S3 bucket named devops-cp-14719 already exists. Copy the file /tmp/devops.txt to s3 bucket devops-cp-14719 using Terraform. The Terraform working directory is /home/bob/terraform. Update the main.tf file (do not create a separate .tf file) to accomplish this task.
The Terraform working directory is /home/bob/terraform.
Note: Right-click under the EXPLORER section in VS Code and select Open in Integrated Terminal to launch the terminal.
# /home/bob/terraform/main.tf
resource "aws_s3_bucket" "my_bucket" {
bucket = "devops-cp-14719"
# Note: The 'acl' argument is deprecated.
# For this specific scenario (using an existing bucket),
# we'll assume the bucket definition is provided for context
# but the object resource is what performs the file copy.
# We will keep the original bucket definition as provided.
acl = "private"
tags = {
Name = "devops-cp-14719"
}
}
# Add this resource to copy the file to the S3 bucket.
resource "aws_s3_object" "devops_file" {
# The key is the desired name of the file in the S3 bucket.
key = "devops.txt"
# The bucket argument specifies the target bucket name.
bucket = aws_s3_bucket.my_bucket.bucket
# The source argument is the local path to the file you want to upload.
source = "/tmp/devops.txt"
# The 'etag' ensures Terraform can detect if the local file changes,
# forcing an update to the S3 object.
etag = filemd5("/tmp/devops.txt")
}
terraform init
# or apply forcefully without creating plan and applying it
terraform apply -auto-approve